/**
 * Created by leezhenbo on 15/5/22.
 */
package screens {
import events.NavigationEvent;

import starling.animation.Transitions;

import starling.animation.Tween;
import starling.core.Starling;

import starling.display.BlendMode;
import starling.display.Button;
import starling.display.Image;
import starling.display.Sprite;
import starling.events.Event;

public class Welcome extends Sprite{
    /**
     * 背景图片
     */
    private var bg:Image;
    /**
     * 标题
     */
    private var title:Image;
    /**
     * 播放按钮
     */
    private var playBtn:Button;

    /**
     * 关于按钮
     */
    private var aboutBtn:Button;

    /**
     * 英雄
     */
    private var hero:Image;

    /** Hero art tween object. */
    private var tween_hero:Tween;

    public function Welcome() {
         this.addEventListener(Event.ADDED_TO_STAGE,onAddedToStageHandler);
    }

    protected function onAddedToStageHandler(event:Event):void{
        this.removeEventListener(Event.ADDED_TO_STAGE,onAddedToStageHandler);
        drawScreen();
    }

    /**
     * 绘制场景
     */
    private function drawScreen(){
        //background elements
        if(!bg){
            bg = new Image(Assets.getTexture("BgWelcome"));
            bg.blendMode = BlendMode.NONE;
            this.addChild(bg);
        }
        if(!title){
            title = new Image(Assets.getTextureAtlas().getTexture("welcome_title"));
            title.x = 600;
            title.y = 65;
            this.addChild(title);
        }
        //welcome elements
        if(!hero){
            hero = new Image(Assets.getTextureAtlas().getTexture("welcome_hero"));
            hero.x = -hero.width;
            hero.y = 130;
            this.addChild(hero);
        }

        if(!playBtn){
            playBtn = new Button(Assets.getTextureAtlas().getTexture("welcome_playButton"));
            playBtn.x = 500;
            playBtn.y = 260;
            playBtn.addEventListener(Event.TRIGGERED,onPlayClick);
            this.addChild(playBtn);
        }
        if(!aboutBtn){
            aboutBtn = new Button(Assets.getTextureAtlas().getTexture("welcome_aboutButton"));
            aboutBtn.x = 410;
            aboutBtn.y = 410;
            this.addChild(aboutBtn);
        }
    }


    /**
     * On play button click.
     * @param event
     *
     */
    private function onPlayClick(event:Event):void
    {
        trace("play click")
        this.dispatchEvent(new NavigationEvent(NavigationEvent.CHANGE_SCREEN, {id: "play"}, true));
    }

    public function initialize():void{
        this.visible = true;

        hero.x = -hero.width;
        hero.y = 100;

        tween_hero = new Tween(hero, 4, Transitions.EASE_OUT);
        tween_hero.animate("x", 80);
        Starling.juggler.add(tween_hero);

        this.addEventListener(Event.ENTER_FRAME, floatingAnimation);
    }

    public function disposeTemporaily():void{
        this.visible = false;
    }

    private var _currentDate:Date;

    private function floatingAnimation():void{
        _currentDate = new Date();
        hero.y = 130 + (Math.cos(_currentDate.getTime() * 0.002)) * 25;
        playBtn.y = 340 + (Math.cos(_currentDate.getTime() * 0.002)) * 10;
        aboutBtn.y = 460 + (Math.cos(_currentDate.getTime() * 0.002)) * 10;
    }
}
}
